//
//  YYETeacherDetailViewController.m
//  YuYingEDU
//
//  Created by zl on 2018/3/12.
//  Copyright © 2018年 胡波. All rights reserved.
//

#import "YYETeacherDetailViewController.h"
#import "YYEHotCourseCollectionViewCell.h"
#import "NSStringExt.h"


@interface YYETeacherDetailViewController ()<UICollectionViewDelegate,UICollectionViewDataSource,UICollectionViewDelegateFlowLayout>

@property (nonatomic,strong) UICollectionView *collectionView;
@property (nonatomic,strong) UICollectionViewFlowLayout *flowLayout;
@property (nonatomic,strong) NSArray *datas;
@property (nonatomic,strong) UIView *tableHeadView;
@property (nonatomic,strong) YYEHomeCourseModel *model;


@end

@implementation YYETeacherDetailViewController

static NSString *hotCourseCell = @"courseCell";


- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    self.view.backgroundColor = [UIColor whiteColor];


    [self loadData];
}




- (void)loadData{
    
    NSDictionary *dic = @{
                          @"tid":self.tid,
                          @"uid":[LoginModel shareLogin].uid,
                          @"username":[LoginModel shareLogin].username,
                          @"token":[LoginModel shareLogin].token,
                          //                          @"p":@(self.page),
                          //                          @"size":@10
                          };
    [SVProgressHUD show];
    [AFNHelper post:TeacherInfo parameters:dic success:^(id responseObject) {
        
        
        if ([responseObject[@"status"] isEqual:@1]) {
            
            self.model = [YYEHomeCourseModel mj_objectWithKeyValues:responseObject[@"data"][@"info"]];
            
            NSArray *commandArr = [YYEHomeCourseModel mj_objectArrayWithKeyValuesArray:responseObject[@"data"][@"lesson"]];
            self.datas = commandArr;

            [self configUI];

        }
        
        
        [SVProgressHUD showWithStatus:responseObject[@"message"]];
        [SVProgressHUD dismiss];
        
    } faliure:^(id error) {
        
        NSLog(@"请求失败");
        [SVProgressHUD showWithStatus:ShowError];
        [SVProgressHUD dismissWithDelay:0.5];
        
    }];
    
}



- (void)configUI {
    
    CGSize size = [NSString textHeight:self.model.intro font:[UIFont systemFontOfSize:12] width:kScreenWidth-48];
    
    self.flowLayout = [[UICollectionViewFlowLayout alloc]init];
    self.flowLayout.minimumLineSpacing = 0;
    self.flowLayout.minimumInteritemSpacing = 0;
    self.flowLayout.scrollDirection = UICollectionViewScrollDirectionVertical;
    self.flowLayout.headerReferenceSize = CGSizeMake(SCREEN_WIDTH, 221+27+size.height+20);  //设置headerView大小

    
    self.collectionView = [[UICollectionView alloc]initWithFrame:CGRectZero collectionViewLayout:self.flowLayout];
    self.collectionView.delegate = self;
    self.collectionView.dataSource = self;
    self.collectionView.showsVerticalScrollIndicator = NO;
    self.collectionView.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:self.collectionView];
    
    [self.collectionView registerClass:[UICollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"HeaderView"];  //  一定要设置
    [self.collectionView registerClass:[YYEHotCourseCollectionViewCell class] forCellWithReuseIdentifier:hotCourseCell];
    
    [self.collectionView makeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(UIEdgeInsetsMake(0, 0, 0, 0));
    }];
    
}

#pragma mark -- UIcollectionView
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    
    return self.datas.count;
//    return 10;

}

- (UICollectionReusableView *) collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath
{
    UICollectionReusableView *headerView = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"HeaderView" forIndexPath:indexPath];
    
    if (!_tableHeadView) {
        
        CGSize size = [NSString textHeight:self.model.intro font:[UIFont systemFontOfSize:12] width:kScreenWidth-48];

        UIView *tableHeadView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 221+27+size.height+20)];
        [headerView addSubview:tableHeadView];
//        tableHeadView.backgroundColor = [UIColor yellowColor];
        _tableHeadView = tableHeadView;
        
        UIImageView *headView = [UIImageView imgViewWithframe:CGRectZero icon:@""];
//        headView.backgroundColor = [UIColor redColor];
        [tableHeadView addSubview:headView];
        [headView sd_setImageWithURL:[NSURL URLWithString:self.model.img]];
        
        UILabel *nameLab = [UILabel labelWithframe:CGRectZero text:self.model.name font:[UIFont systemFontOfSize:14] textAlignment:NSTextAlignmentCenter textColor:@"#030303"];
        [tableHeadView addSubview:nameLab];
        
        UILabel *roleLab = [UILabel labelWithframe:CGRectZero text:self.model.role font:[UIFont systemFontOfSize:12] textAlignment:NSTextAlignmentCenter textColor:@"#151515"];
        [tableHeadView addSubview:roleLab];
        
        UIView *hline1 = [[UIView alloc] init];
        hline1.backgroundColor = [UIColor colorWithHexString:@"#EDEDED"];
        [tableHeadView addSubview:hline1];
        
        UIView *vline1 = [[UIView alloc] init];
        vline1.backgroundColor = [UIColor colorWithHexString:@"#030303"];
        [tableHeadView addSubview:vline1];
        
        UILabel *introduceLab = [UILabel labelWithframe:CGRectZero text:@"介绍" font:[UIFont systemFontOfSize:14] textAlignment:0 textColor:@"#030303"];
        [tableHeadView addSubview:introduceLab];
        
        UILabel *contentLab = [UILabel labelWithframe:CGRectZero text:self.model.intro font:[UIFont systemFontOfSize:12] textAlignment:0 textColor:@"#030303"];
        [tableHeadView addSubview:contentLab];
        contentLab.numberOfLines = 0;
        
        UIView *hline2 = [[UIView alloc] init];
        hline2.backgroundColor = [UIColor colorWithHexString:@"#EDEDED"];
        [tableHeadView addSubview:hline2];
        
        //    [tableHeadView makeConstraints:^(MASConstraintMaker *make) {
        //
        //        make.left.right.top.equalTo(self.view);
        //
        //    }];
        
        [headView makeConstraints:^(MASConstraintMaker *make) {
            make.centerX.equalTo(tableHeadView.centerX);
            make.top.equalTo(tableHeadView.top).offset(21);
            make.size.equalTo(CGSizeMake(72, 72));
        }];
        headView.layer.cornerRadius = 72/2;
        
        [nameLab makeConstraints:^(MASConstraintMaker *make) {
            make.centerX.equalTo(tableHeadView.centerX);
            make.top.equalTo(headView.bottom).offset(20);
        }];
        
        [roleLab makeConstraints:^(MASConstraintMaker *make) {
            make.centerX.equalTo(tableHeadView.centerX);
            make.top.equalTo(nameLab.bottom).offset(12);
        }];
        
        [hline1 makeConstraints:^(MASConstraintMaker *make) {
            make.left.right.equalTo(tableHeadView);
            make.top.equalTo(roleLab.bottom).offset(27);
            make.height.equalTo(6);
        }];
        
        [vline1 makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(tableHeadView.left).offset(20);
            make.top.equalTo(hline1.bottom).offset(16);
            make.size.equalTo(CGSizeMake(4, 15));
        }];
        
        [introduceLab makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(vline1.right).offset(7);
            make.top.equalTo(vline1.top);
        }];
        
        [contentLab makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(tableHeadView.left).offset(24);
            make.right.equalTo(tableHeadView.right).offset(-24);
            make.top.equalTo(introduceLab.bottom).offset(9);
        }];
        
        [hline2 makeConstraints:^(MASConstraintMaker *make) {
            make.left.right.equalTo(tableHeadView);
            make.top.equalTo(contentLab.bottom).offset(21);
            make.height.equalTo(6);
        }];
        
        //    // 最后算高度
        //    [tableHeadView makeConstraints:^(MASConstraintMaker *make) {
        //
        //        make.bottom.equalTo(hline2.bottom);
        //
        //    }];
    }
    
    return headerView;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    
    YYEHotCourseCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:hotCourseCell forIndexPath:indexPath];
    cell.model = self.datas[indexPath.item];
    return cell;
    
}

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath {
    
    return CGSizeMake(SCREEN_WIDTH / 2 - 3, 71 + (SCREEN_WIDTH / 2 - 6) * 0.55);
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
